package com.moon.back.service.impl.file;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.moon.back.dao.FileNodeDao;
import com.moon.back.entity.Req.FileQueryReq;
import com.moon.back.entity.Req.FileSingleQueryReq;
import com.moon.back.entity.Resp.FileQueryResp;
import com.moon.back.entity.file.FileNode;
import com.moon.back.service.IFileSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文件搜索功能实现
 */
@Service
public class FileSearchServiceImpl implements IFileSearchService {

    /**
     * 文件搜索功能 可能要使用 redisSearch (待考虑)
     * @return
     */
    @Autowired
    private FileNodeDao fileNodeDao;

    @Override
    public SaResult FileSearch(FileQueryReq fileQueryReq) {

        FileQueryResp fileQueryResp = new FileQueryResp();
        Integer startIndex = getFilePageStartIndex(fileQueryReq.getPageNum(),fileQueryReq.getPageSize());
        String searchValue = fileQueryReq.getSearchValue();

        List<FileNode> fileNodes;
        if(BooleanUtil.isTrue(fileQueryReq.getSelectFlag())){
             fileNodes = fileNodeDao.createQuery()
                    .andLike("file_path", "%"+searchValue+"%")
                    .limit(startIndex,fileQueryReq.getPageSize())
                    .select();
        }else{
            fileNodes = fileNodeDao.createQuery()
                    .andLike("file_name","%"+searchValue+"%")
                    .limit(startIndex,fileQueryReq.getPageSize())
                    .select();
        }

        fileQueryResp.setDataList(fileNodes);
        fileQueryResp.setPageNum(fileQueryReq.getPageNum());
        fileQueryResp.setPageSize(fileQueryReq.getPageSize());
        setTotal(fileQueryResp,fileQueryReq);

        return SaResult.data(fileQueryResp);
    }

    @Override
    public SaResult searchSingleById(FileSingleQueryReq fileSingleQueryReq) {

        List<FileNode> fileNode = fileNodeDao.createQuery()
                .andEq("file_id", fileSingleQueryReq.getFileId())
                .select();

        if(ObjectUtil.isNotNull(fileNode)){
            return SaResult.data(fileNode.get(0));
        }

        return SaResult.data(List.of());
    }


    /**
     * 获取分页总数
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Integer getFilePageStartIndex(Integer pageNum,Integer pageSize) {
        return  (pageNum-1)*pageSize+1;
    }

    /**
     *   获取分页总数
     */
    public void setTotal(FileQueryResp fileQueryResp , FileQueryReq fileQueryReq) {
        String searchValue = fileQueryReq.getSearchValue();

        long totalFileNum = 0L;


        if(BooleanUtil.isTrue(fileQueryReq.getSelectFlag())){
            totalFileNum = fileNodeDao.createQuery()
                    .andLike("file_path", "%"+searchValue+"%")
                    .count();
        }else{
            totalFileNum = fileNodeDao.createQuery()
                    .andLike("file_name","%"+searchValue+"%")
                    .count();
        }

        fileQueryResp.setTotal(totalFileNum);
    }

}
